package com.inifitness.common.websocket;

import jakarta.annotation.Nonnull;
import jakarta.websocket.server.ServerEndpointConfig;

import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * WebSocket自定义访问路径配置
 *
 * @author sunjinfeng
 * @version 1.0.0
 * @since 2024/04/18 11:00:00
 */
@Log4j2
public class CustomEndpointConfigure extends ServerEndpointConfig.Configurator implements
    ApplicationContextAware {

  private static ApplicationContext applicationContext;

  @Override
  public <T> T getEndpointInstance(Class<T> clazz) {
    log.debug("-----------------");
    log.debug("context={}", applicationContext);
    log.debug("class={}", clazz);
    log.debug("context.getBean(clazz)={}", applicationContext.getBean(clazz));
    return applicationContext.getBean(clazz);
  }

  @Override
  public synchronized void setApplicationContext(@Nonnull ApplicationContext applicationContext)
      throws BeansException {
    log.debug("MyEndpointConfigure.context注入");
    log.debug("applicationContext={}", applicationContext);
    CustomEndpointConfigure.applicationContext = applicationContext;
  }
}
